<template>
	<view class="trusteeship">
		<view class="nav_bar">
			<uni-nav-bar statusBar fixed :border="false" @clickLeft="back">
				<block slot="left">
					<view class="back_btn">
						<uni-icons type="back" color="#fff" size="20"></uni-icons>
					</view>
				</block>
			</uni-nav-bar>
		</view>
		<view class="banner">
			<image :src="topImgList.pic" mode=""></image>
			<view class="">
				<view class="title">{{topImgList.title}}</view>
				<view class="desc">{{topImgList.content}}</view>
				<view class="tabs">
					<block v-for="item in tabs" :key="item.val">
						<view :class="{'active':currentType==item.val}" @tap="changeTab(item.val)">{{item.name}}</view>
					</block>
				</view>
			</view>
		</view>
		<!-- 托管日期 -->
		<view class="date_content" v-if="currentType==1">
			<view class="custom-calendar">
				<!-- 	<view class="current_date">
					<text>{{currentYear}}</text>
					<view class="line"></view>
					<text>{{currentDate}}</text>
				</view> -->
				<ren-calendar ref='ren' :markDays='markDays' :headerBar='true' @onDayClick="getSelectDate()"></ren-calendar>
			</view>
			<view class="list">
				<block v-for=" (item,i) in list" :key="i">
					<view class="item" @tap="courseDetail(item.id)">
						<view class="info">
							<image class="cover_img" :src="item.image" mode="aspectFill"></image>
							<view class="">
								<view class="title">{{item.storeName}}</view>
								<view class="time">时间：{{item.activityStartTime}}</view>
								<view class="desc">
									{{item.storeInfo }}
								</view>

							</view>
						</view>
						<view class="other_info">
							<view class="address">
								<uni-icons type="location-filled" color="#999999" size="16"></uni-icons>
								<text>{{item.address}}</text>
							</view>
							<view class="price">
								<text>￥</text>
								<text class="pirce_val">{{item.price || '0.00'}}</text>
							</view>
						</view>
						<image v-if="item.type" class="buy_status" src="/static/home/goumai.png" mode="">
						</image>
					</view>
				</block>
			</view>
		</view>
		<!-- 托管师资 -->
		<view class="teacher_list" v-else-if="currentType==2">
			<block v-for="(item,i) in list" :key="i">
				<view class="item">
					<view class="teacher_name">{{item.teacherName}}</view>
					<view class="teacher_info">
						<view class="desc">
							<text class="tag">老师简介</text>
							<text>{{item.introduce}}</text>
						</view>
						<image :src="item.cover" mode="aspectFill"></image>
					</view>
				</view>
			</block>

		</view>
		<!-- 托管风采 -->
		<view class="article_list" v-else>
			<block v-for="(item,i) in list" :key="i">
				<view class="item" @click="toDetail(item)">
					<view class="item_t">
						<view class="author">
							<image :src="item.avatar" mode="aspectFill"></image>
							<view class="">
								<view class="author_name">{{item.realName}}</view>
								<view class="time">{{item.createTime}}</view>
							</view>
						</view>
					</view>
					<view class="item_m">
						<view class="article_text">
							<rich-text :nodes="item.content"></rich-text>
						</view>
						<view class="article_imgs">
							<block v-for="(img,idx) in item.imgs" :key="idx">
								<image :src="img" mode="aspectFill" @tap="previewImg(idx,item.imgs)"></image>
							</block>
						</view>
					</view>
					<view class="item_b">浏览量{{item.views}}</view>
				</view>
			</block>
		</view>
		<view style="text-align: center;" v-if="list.length==0">
			<image style="width: 200rpx;height: 200rpx;" src="../../../static/nodata.png"></image>
			<view style="color: #999;text-align: center;">暂无数据</view>
		</view>
		<view style="text-align: center;color: #999;font-size: 24rpx;" v-if="noMoreData&&list.length!=0">
			没有更多啦
		</view>
	</view>
</template>

<script>
	import RenCalendar from '@/components/ren-calendar/ren-calendar.vue'
	export default {
		components: {
			RenCalendar
		},
		data() {
			return {
				topImgList:[],
				topMsg:{},
				tabs: [
					{
						name: '托管日期',
						val: 1
					}, {
						name: '托管师资',
						val: 2
					}, {
						name: '托管风采',
						val: 3
					},
				],
				currentType: 1,
				page: 1,
				markDays: [],
				list: [],
				lastPage: 1,
				noMoreData: false,
				startTime: '', //筛选的年月
				startDate: '',
				endDate: '',
				images: []
			};
		},
		async onLoad() {
			this.getTopImg()
			uni.$on('changeTime', async (data) => {
				this.startTime = data
				this.page = 1;
			})
		},
		onShow() {
			if(this.startTime) {
				this.estimate()
				// this.page = 1
				// this.getCourse()
			}
		},
		onReachBottom() {
			if (this.page < this.lastPage) {
				this.page+=1
				this.estimate()
			} else {
				this.noMoreData = true
			}
		},
		methods: {
			previewImg(current,urls) {
				uni.previewImage({
					current,
					urls,
				});
			},
			async getTopImg() {
				// 0首页 1研学 2冬夏令营 3红领巾 4托管 5心理测评
				const res = await this.$api.get(`image/get/4`, {});
				console.log(res, "红领巾")
				if (res.code == 200) {
					// this.topMsg = res.data
					// this.topImgList = res.data.image.split(',')
					this.topImgList = res.data.bastBanner[0]
				}
			},
			getSelectDate(e){
				// console.log(e,"eeee---")
				this.startTime=e.date;
				// this.startDate = decodeURIComponent(`${e.date} 00:00:00`);
				// this.endDate = decodeURIComponent(`${e.date} 23:59:59`);
				this.getCourse()
			},
			getImg(image){
				return image.split(',')
			},
			// 托管课程
			async getCourse() {
				const res = await this.$api.get('ebTrusteeshipCourse/list', {
					page: this.page,
					limit: 10,
					startTime: this.startTime,
					endTime: this.startTime
				});
				if (res.code == 200) {
					console.log(res, "res--")
					const { list,totalPage } = res.data;
					
					if (this.page == 1) {
						this.list = list || [];
						this.lastPage = totalPage || 1;
					} else {
						this.list.push(...list);
					}
					this.page != 1 && list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}

			},
			// 托管日历
			async getCalendar() {
				const res = await this.$api.get('ebTrusteeshipCourse/datePurchase', {
					startTime: this.startTime
				});
				if (res.code == 200) {
					const { data,totalPage } = res
					if(data!=null||data?.length>=1){
						this.markDays = data.map(item => {
							return item.slice(0, 10)
						})
					}
					this.getCourse()
				}
			},
			changeTab(val) {
				this.currentType = val
				this.page = 1
				this.list = []
				this.noMoreData = false
				this.estimate()
			},
			async estimate() {
				if (this.currentType == 1) {
					await this.getCalendar();
				}
				if (this.currentType == 2) {
					this.getTeacherInformation()
				}
				if (this.currentType == 3) {
					this.getTeacherInMien()
				}
			},
			// 托管风采 
			async getTeacherInMien() {
				const res = await this.$api.get('ebTrusteeshipStyle/list', {
					page: this.page,
					pagesize: 10,
				});
				console.log(res, "11111--")
				if (res.code == 200) {
					const { list, totalPage } = res.data;
					list.forEach(v => {
						if(v.image) {
							var str = v.image.replace(/\[/g, '');
							str = str.replace(/]/g,'');
							str = str.replace(/"/g,'');
							v.image = str;
							v.imgs = str.split(',');
							// console.log(v.imgs,str)
						}else {
							v.imgs = []
						}
					})
					if (this.page == 1) {
						this.list = list || [];
						this.lastPage = totalPage || 1;
						// console.log(this.list,'this.list')
					} else {
						this.list.push(...list);
					}
					this.page != 1 && list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}

			},
			// 托管师资
			async getTeacherInformation() {
				const res = await this.$api.get('ebTeacher/list', {
					page: this.page,
					limit: 10,
				});
				if (res.code == 200) {
					// console.log(res, "res--")
					const {
						list,
						totalPage
					} = res.data
					if (this.page == 1) {
						this.list = list || [];
						this.lastPage = totalPage || 1;
					} else {
						this.list.push(...list);
					}
					this.page != 1 && list.length == 0 ? this.noMoreData = true : this.noMoreData = false
				}

			},
			courseDetail(id) {
				uni.navigateTo({
					url: `/pages/index/gameDetail/gameDetail?id=${id}&type=6`
				})
			},
			toDetail(v) {
				uni.navigateTo({
					url: `/pages/index/workShowDetail?type=1&id=${v.id}`
				})
			}
		},
		async onReady() {
			// var now = new Date();
			// var year = now.getFullYear();
			// var month = now.getMonth() + 1;
			// var day = now.getDate();
			// this.startTime = `${year}-${month}-${day}`
			// console.log(this.startTime);
			// this.startDate = decodeURIComponent(`${year}-${month}-${day} 00:00:00`);
			// this.endDate = decodeURIComponent(`${year}-${month}-${day} 23:59:59`);
			this.estimate()
		},
	}
</script>

<style lang="scss">
	page {
		background-color: #f7f7f7;
	}
	
	p {
		text-indent: 2em;
	}

	.trusteeship {
		.nav_bar {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			z-index: 99;

			/deep/.uni-navbar {
				// opacity: 0;
				.uni-navbar__content {
					background-color: transparent !important;

					.uni-navbar__header {
						background-color: transparent !important;
					}
				}
			}

			.back_btn {
				width: 54rpx;
				height: 54rpx;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: rgba(0, 0, 0, 0.4);
			}
		}

		.banner {
			width: 100%;
			height: 460rpx;
			position: relative;
			margin-bottom: 76rpx;

			image {
				width: 100%;
				height: 460rpx;
			}

			>view {
				width: 100%;
				height: 100%;
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				z-index: 2;
				padding: 186rpx 26rpx 0;
				box-sizing: border-box;

				.title {
					font-size: 52rpx;
					font-weight: bold;
					color: #fff;
					line-height: 52rpx;
				}

				.desc {
					font-size: 24rpx;
					color: #fff;
					padding: 40rpx 0 46rpx;
				}


				.tabs {
					width: 100%;
					display: flex;
					justify-content: space-between;

					>view {
						width: 212rpx;
						height: 80rpx;
						background-color: #fff;
						text-align: center;
						line-height: 80rpx;
						font-size: 32rpx;
						font-weight: bold;
						border-radius: 14rpx;
					}

					.active {
						color: #5B2E00;
						background-color: #FFEE8B;
						position: relative;
					}

					.active::before {
						height: 0;
						width: 0;
						border-left: 20rpx solid transparent;
						border-right: 20rpx solid transparent;
						border-top: 20rpx solid #FFEE8B;
						position: absolute;
						display: block;
						content: '';
						bottom: -16rpx;
						left: 86rpx;
					}
				}
			}
		}

		.date_content {
			padding: 0 26rpx;
			box-sizing: border-box;

			.custom-calendar {
				width: 100%;
				background-color: #fff;
				border-radius: 20rpx;
				overflow: hidden;

				.current_date {
					display: flex;
					justify-content: center;
					align-items: center;
					padding: 36rpx 0 6rpx;

					text {
						font-size: 26rpx;
						font-weight: bold;
					}

					.line {
						width: 2rpx;
						height: 24rpx;
						background-color: #fff;
						margin: 0 26rpx;
					}
				}
			}

			.list {
				padding: 20rpx 26rpx 30rpx;
				box-sizing: border-box;

				.item {
					width: 100%;
					padding: 20rpx 26rpx 26rpx;
					box-sizing: border-box;
					background-color: #fff;
					border-radius: 20rpx;
					margin-bottom: 20rpx;
					position: relative;

					.info {
						display: flex;
						align-items: flex-start;
						padding-bottom: 14rpx;
						border-bottom: 1rpx dashed #F0F0F0;
						margin-bottom: 26rpx;

						.cover_img {
							width: 200rpx;
							height: 160rpx;
							border-radius: 14rpx;
							margin-right: 16rpx;
						}

						>view {
							flex: 1;

							.title {
								width: 300rpx;
								font-size: 30rpx;
								line-height: 30rpx;
								font-weight: bold;
								color: #1A1A1A;
								overflow: hidden;
								white-space: nowrap;
								text-overflow: ellipsis;
							}

							.time {
								font-size: 24rpx;
								line-height: 24rpx;
								color: #999999;
								padding: 30rpx 0 18rpx;
							}

							.desc {
								font-size: 24rpx;
								line-height: 24rpx;
								color: #999999;
							}


						}
					}

					.other_info {
						display: flex;
						justify-content: space-between;

						.address {
							display: flex;
							align-items: center;
							font-size: 24rpx;
							color: #999999;
							line-height: 24rpx;
						}

						.price {
							font-size: 24rpx;
							font-weight: bold;
							color: #FF0404;

							.pirce_val {
								font-size: 34rpx;
								color: #FF0000;
							}
						}
					}

					.buy_status {
						position: absolute;
						top: 20rpx;
						right: 18rpx;
						width: 102rpx;
						height: 58rpx;
					}
				}
			}
		}

		.teacher_list {
			padding: 0 26rpx 50rpx;
			box-sizing: border-box;

			.item {
				width: 100%;
				height: 200rpx;
				background-color: #fff;
				border-radius: 14rpx;
				padding: 20rpx 24rpx 0;
				box-sizing: border-box;
				margin-bottom: 20rpx;

				.teacher_name {
					font-size: 30rpx;
					font-weight: bold;
					line-height: 30rpx;
					margin-bottom: 10rpx;
				}

				.teacher_info {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.desc {
						font-size: 24rpx;
						line-height: 32rpx;
						flex: 1;
						margin-right: 60rpx;

						.tag {
							font-size: 20rpx;
							line-height: 20rpx;
							color: #2E80FE;
							background-color: #E4EFFF;
							padding: 6rpx 10rpx;
						}
					}

					image {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
					}
				}
			}
		}

		.article_list {
			padding: 0 26rpx;
			box-sizing: border-box;

			.item {
				padding: 16rpx 26rpx 20rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
				background-color: #fff;
				margin-bottom: 20rpx;
				position: relative;

				.item_t {
					display: flex;
					justify-content: space-between;

					.author {
						display: flex;
						align-items: center;

						image {
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
							margin-right: 14rpx;
						}

						.author_name {
							font-size: 30rpx;
							line-height: 30rpx;
							font-weight: bold;
							margin-bottom: 14rpx;
						}

						.time {
							font-size: 24rpx;
							line-height: 24rpx;
							color: #B3B3B3;
						}
					}
				}

				.item_m {
					padding: 20rpx 0;

					.article_text {
						font-size: 26rpx;
						line-height: 40rpx;
						margin-bottom: 16rpx;
					}

					.article_imgs {
						display: grid;
						grid-template-columns: repeat(3,1fr);
						image {
							width: 200rpx;
							height: 200rpx;
							border-radius: 20rpx;
							margin-bottom: 20rpx;
						}

						image:not(:last-child) {
							margin-right: 12rpx;
						}
					}
				}

				.item_b {
					font-size: 24rpx;
					color: #999999;
					line-height: 24rpx;
				}
			}
		}
	}
</style>